package com.kanq.common.utils;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.seg.common.Term;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class HanlpUtils {

    public static List<String> hanLPSegment(String text) {
        HashSet<String> wordList = new HashSet<>();
        List<Term> words = HanLP.segment(text);
        for (Term tm : words) {
            if (tm.nature == Nature.n || tm.nature == Nature.vn) {
                wordList.add(tm.word);
            }
        }

        return wordList.stream().toList();
    }

    /**
     * 提取文本中的句子
     *
     * @param text 输入文本
     * @return 句子列表
     */
    public static List<String> extractSentences(String text) {
        if (text == null || text.isEmpty()) {
            return new ArrayList<>();
        }

        // 使用HanLP的句子分割功能
        List<String> sentences = HanLP.extractSummary(text, Integer.MAX_VALUE)
                .stream()
                .map(s -> s.trim())
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());

        return sentences;
    }

    public static String extractSummary(String text, int length) {
        return HanLP.getSummary(text, length);
    }

    public static void main(String[] args) {
        System.out.println(HanLP.getSummary("你好", 5));
    }
}
